package 快递单号查询;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhuQianli on 2018/1/27.
 */
public class App {
    enum ExpressType {
        zhongtong,tiantian,yuantong,yunda
    }


    public static void main(String[] args) throws IOException {
        ExpressType expressType = ExpressType.yunda;
        String oid = "3901072119581";

        search(expressType, oid);


    }

    private static void yuantong() throws IOException {
        ExpressType expressType = ExpressType.yuantong;
        String oid = "888106786647979864";

        search(expressType, oid);
    }

    private static void tiantian() throws IOException {
        ExpressType expressType = ExpressType.tiantian;
        String oid = "123123123123";

        search(expressType, oid);
    }

    private static void zhongtong() throws IOException {
        ExpressType expressType = ExpressType.zhongtong;
        String oid = "475821266590";

        search(expressType, oid);
    }


    private static void search(ExpressType expressType, String oid) throws IOException {
        String s = sendGet("https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv/pae/channel/data/asyncqury?cb=&appid=4001&com=" + expressType.name() + "&nu="+oid+"&vcode=&token=&_=1517030288683");
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONObject info = data.getJSONObject("info");
        System.out.println(JSON.toJSONString(info, true));
    }

    private static String sendGet(String url) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpGet httpGet = new HttpGet(url);

        Header[] headers = createHeaders();
        httpGet.setHeaders(headers);

        StringBuilder stringBuilder = new StringBuilder();
        try (CloseableHttpResponse execute = client.execute(httpGet)) {
            HttpEntity entity = execute.getEntity();
            InputStream content = entity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content, "GBK"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();


    }

    private static Header[] createHeaders() {
        List headersList = new ArrayList();
        headersList.add(new BasicHeader("accept", "*/*"));
        headersList.add(new BasicHeader("accept-encoding", "gzip, deflate, br"));
        headersList.add(new BasicHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8"));
        headersList.add(new BasicHeader("cookie", "BIDUPSID=B37CBB10356FDD6D7752ECE868AAC451; BAIDUID=685310C1BF5620A22B63B8D0C22CD05D:FG=1; PSTM=1503038583; BDUSS=1haaFJKWkxDblNrLVF1ajFpdn5ydDZUZjRSYnN4V24yZXJ2SU1sSm1ub25HY0paTVFBQUFBJCQAAAAAAAAAAAEAAACAeV87cTEwNzI5OTMyMDcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACeMmlknjJpZc; __cfduid=dbe9256acc2f46882b64d4eccae230e881508308515; BDSFRCVID=I7tsJeCCxG3ukHcAVrf_WlxftBIS0_P0eoty3J; H_BDCLCKID_SF=JbAjoKK5tKvbfP0kh-QJhnQH-UnLqMFfbT7Z0lOnMp05fJvS-UQ4K4Km3UvOKJ3GWGAjMMtM5R7MJDO_e4bK-TrLjNAfJf5; locale=zh; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=5; H_PS_PSSID=1445_21121_18560; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598"));
        headersList.add(new BasicHeader("Host", "sp0.baidu.com"));
        headersList.add(new BasicHeader("Referer", "https://www.baidu.com/s"));
        headersList.add(new BasicHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"));

        return (Header[]) headersList.toArray(new Header[headersList.size()]);
    }
}
